package com.hua.api.modules.home.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.constants.Status;
import com.hua.api.core.framework.sms.SmsWrapper;
import com.hua.api.core.util.StringUtil;
import com.hua.api.core.util.ValidationUtil;
import com.hua.api.modules.home.dao.MemberInfoDao;
import com.hua.api.modules.home.domain.MemberInfo;
import com.hua.api.modules.home.service.MessageService;
import com.hua.api.modules.order.dao.OrderInfoDao;
import com.hua.api.modules.order.domain.OrderInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

// 短信类服务
@Slf4j
@Service("smsMessageService")
@RequiredArgsConstructor
public class SmsMessageServiceImpl implements MessageService {

    private final OrderInfoDao orderInfoDao;

    private final MemberInfoDao memberInfoDao;


    @Override
    public void sendMessage(JSONObject data,String event) {

        String orderNo = data.getString("order_no");
        ValidationUtil.requireNotNull(orderNo,"订单号不存在！");
        OrderInfo orderInfo = orderInfoDao.findOrderInfoWithOrderNo(orderNo);
        ValidationUtil.requireNotNull(orderInfo,"订单数据不存在！");

        if(StringUtil.equals(Status.SOURCE_WECHAT,orderInfo.getSource())){
            MemberInfo member = orderInfoDao.getObject(MemberInfo.class,orderInfo.getUserId());
            ValidationUtil.requireNotNull(member,"会员信息不存在！");
            // 通知用户
            SmsWrapper.sendSmsOnOrderStatusChange(member.getPhoneNumber(),orderInfo.getOrderNo(),"已接单");
            // 通知运营人员
            SmsWrapper.sendOperater("13522328241",orderInfo.getOrderNo());

        }
    }

}
